package com.dh.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dh.common.ResultMapUtil;
import com.dh.pojo.Owinfo;
import com.dh.service.OwinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * 药品出入库相关的controller
 */
@Controller
@RequestMapping(value = "/owinfo")
public class OwinfoController {

    @Autowired
    private OwinfoService owinfoService;

    /**
     * 转向药品出入库页面
     */
    @RequestMapping
    public String owinfo(){
        return "/owinfo";
    }


    /**
     * 分页查询药品出入库列表
     * value可以指定请求的参数
     * value = "/xxx",method = RequestMethod.POST
     * @param param 查询参数
     * @param pageNum 起始页数
     * @param pageSize 每页多少条数据
     * @return
     */
    @RequestMapping(value = "/owinfoQueryPage")
    @ResponseBody
    public Object owinfoQueryPage(String param, @RequestParam(defaultValue = "1")int pageNum,@RequestParam(defaultValue = "10")int pageSize){
        try{
            //查询对应的数据
            IPage<Owinfo> iPage = owinfoService.selectOwinfoPage(pageNum,pageSize,param);
            //获取mysql的分页查询结果
            return ResultMapUtil.getHashMapMysqlPage(iPage);
        } catch (Exception e) {
            //如果出错，把异常信息返回给前端
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 转向药品出入库新增页面
     * @return 跳转owinfoPage.html
     */
    @RequestMapping(value = "/owinfoPage")
    public String owinfoPage(){
        return "/owinfoPage";
    }

    /**
     * 添加一个药品出入库
     * @param owinfo
     * @return
     */
    @RequestMapping(value = "/owinfoAdd")
    @ResponseBody
    public Object owinfoAdd(Owinfo owinfo){
        try {
            //获取创建时间
            owinfo.setCreatetime(new Date());
            //新增一条出入库信息
            int i = owinfoService.addOwinfo(owinfo);
            //保存,并统一格式返回
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            //如果出错，把异常信息返回给前端
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 根据查询的id，转向药品出入库编辑页面
     * @param id 查询的id
     * @param model
     * @return
     */
    @RequestMapping(value = "/owinfoQueryById")
    public String owinfoQueryById(@RequestParam(name = "id",required = true)Integer id, Model model){
        //根据id查询药品
        Owinfo owinfo = owinfoService.queryOwinfoById(id);
        //model调用逻辑处理
        model.addAttribute("obj", owinfo);
        //跳转页owinfoPage.html
        return "/owinfoPage";
    }

    /**
     * 修改一个药品出入库
     * @param owinfo 实体类
     * @return 返回修改的数据
     */
    @RequestMapping(value = "/owinfoEdit")
    @ResponseBody
    public Object owinfoupdate(Owinfo owinfo){
        try {
            //修改一条药品出入库信息
            int i = owinfoService.editOwinfo(owinfo);
            //保存,并统一格式返回
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            //如果出错，把异常信息返回给前端
           return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 删除一个药品出入库
     * @param id 根据id删除
     * @return 返回删除的数据
     */
    @RequestMapping(value = "/owinfoDelById")
    @ResponseBody
    public Object owinfoDelById(Integer id){
        try {
            //根据id删除数据
            int i = owinfoService.delOwinfoById(id);
            //保存,并统一格式返回
            return ResultMapUtil.getHashMapDel(i);
        } catch (Exception e) {
            //如果出错，把异常信息返回给前端
            return ResultMapUtil.getHashMapException(e);
        }
    }
}